package com.cuzofu.fs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuzofu.fs.common.R;
import com.cuzofu.fs.entity.BlueprintHistoryEntity;
import com.cuzofu.fs.service.BlueprintHistoryService;
import com.cuzofu.fs.vo.BlueprintHistoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图纸版本历史
 *
 * @author cuzofu
 * @email cuzofu@qq.com
 * @date 2020-07-15 08:47:51
 */
@RestController
@RequestMapping("blueprinthistory")
public class BlueprintHistoryController {
    @Autowired
    private BlueprintHistoryService blueprintHistoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam String bid){
        QueryWrapper<BlueprintHistoryEntity> qw = new QueryWrapper<>();
        qw.eq("bid", bid);
        qw.orderByDesc("version");
        List<BlueprintHistoryEntity> list = blueprintHistoryService.list(qw);

        return R.ok().put("list", list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/issue")
    public R listIssue(BlueprintHistoryVO vo){
        List<BlueprintHistoryVO> list = blueprintHistoryService.listContainsIssue(vo);

        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		BlueprintHistoryEntity blueprintHistory = blueprintHistoryService.getById(id);

        return R.ok().put("blueprintHistory", blueprintHistory);
    }

    /**
     * 比对数据源信息
     * @param mId 修改后图纸ID
     * @param oId 修改前图纸ID
     * @return
     */
    @RequestMapping("/compareInfo/{mId}/{oId}")
    public R compareInfo(@PathVariable("mId") String mId, @PathVariable("oId") String oId){
        // 修改后
        BlueprintHistoryEntity modify = blueprintHistoryService.getById(mId);
        // 修改前
        BlueprintHistoryEntity original = blueprintHistoryService.getById(oId);

        Map<String, Object> result = new HashMap<>();
        result.put("modify", modify);
        result.put("original", original);
        return R.ok().put("data", result);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BlueprintHistoryEntity blueprintHistory){
		blueprintHistoryService.save(blueprintHistory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BlueprintHistoryEntity blueprintHistory){
		blueprintHistoryService.updateById(blueprintHistory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
		blueprintHistoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
